package xyz.xpman.prisonerfitness.sqlite;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import org.jetbrains.annotations.NotNull;

import xyz.xpman.prisonerfitness.entity.Condition;
import xyz.xpman.prisonerfitness.util.StrKit;

public class SQLiteDatabaseHelper {
    private static SQLiteDatabase db;

    public static void by(@NotNull SQLiteDatabase db) {
        SQLiteDatabaseHelper.db = db;
    }




    public Cursor query(@NotNull Condition condition) {
        if (null == condition.isDistinct()) {
            if (StrKit.notBlank(condition.getLimit())) {
                return db.query(condition.getTable(), condition.getColumn(),
                        condition.getSelection(), condition.getSelectionArgs(), condition.getGroupB(),
                        condition.getHaving(), condition.getOrderBy(), condition.getLimit());
            } else {
                return db.query(condition.getTable(), condition.getColumn(),
                        condition.getSelection(), condition.getSelectionArgs(), condition.getGroupB(),
                        condition.getHaving(), condition.getOrderBy());
            }
        } else {
            if (null != condition.getCancellationSign()) {
                return db.query(condition.isDistinct(), condition.getTable(), condition.getColumn(),
                        condition.getSelection(), condition.getSelectionArgs(), condition.getGroupB(),
                        condition.getHaving(), condition.getOrderBy(), condition.getLimit(), condition.getCancellationSign());
            } else if (null != condition.getLimit()) {
                return db.query(condition.isDistinct(), condition.getTable(), condition.getColumn(),
                        condition.getSelection(), condition.getSelectionArgs(), condition.getGroupB(),
                        condition.getHaving(), condition.getOrderBy(), condition.getLimit());
            }
        }
        return null;
    }


}
